include("message.js");

karotz.connectAndStart = function(host, port, callback, data){	
    try{
        karotz.connect(host, port);
        log("connected");
        karotz.start(callback, data);
    }catch(err){
        log(err);
    }
};


karotz.message = function(text, args){
    var isToken = false;
    var buffer = "";
    var bufferToken = "";
    log("message : "+text);
    try{
        for(var i=0; i<text.length; i++){
            if(text[i] != '{' && text[i] != '}'){
                if(isToken){
                    bufferToken = bufferToken + text[i];
                }else{
                    buffer = buffer + text[i];
                }
            }
        
            if(text[i] == '{'){
                isToken = true;
            }
            if(text[i] == '}'){
                isToken = false;
                buffer = buffer+args[bufferToken];
                bufferToken = "";
            }
        }
    }catch(err){
        log(err);
    }
    return buffer;
}


